import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {

    private ServerSocket serverSocket;


    public Server(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }

    public void start() throws IOException {
        ExecutorService es = Executors.newCachedThreadPool();
        // 不断等待新的客户端连接
        while (!serverSocket.isClosed()) {
            System.out.println("等待连接....");
            Socket socket = serverSocket.accept();
            System.out.println("有客户端连接成功");
            // 每当一个客户端连接进来就创建一个新的线程负责接收并转发来自这个客户端的消息
            es.execute(new ServerThread(socket));
            System.out.println("有一个客户端开始了考试线程");
        }
    }
}


